package day12;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//测试单线程池
public class TestSingle {
    static volatile int num = 100;   //总票数100

    public static void main(String[] args) {
        //1.创建
        ExecutorService es = Executors.newSingleThreadExecutor();
        //2.执行
        //es.execute(Runnable);   //void
        //es.submit(Callable或Runnable);   //Future获取call返回值
        for (int i = 0; i < 1000; i++) {
            es.execute(() -> {
                String name = Thread.currentThread().getName();
                num--;
                System.out.println(name + "售出了第" + (100 - num) + "张票,剩余:" + num);
                //判断num = 0
                if (num == 0) es.shutdownNow();
            });
        }

        //3.关闭
        //关闭线程池，如果里面已经有添加的任务还会执行(不会执行execute()添加任务了)
        //es.shutdown();
        //关闭线程池，立即关闭线程池，凡是没有执行的任务 则不会执行
        //es.shutdownNow();
    }
}
